//
//  OriginalView.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/20.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class OriginalView: UIView {
    
    var bottomConstraint: Constraint?
    
    var statusViewModel: CellStatusViewModel?{
        
        didSet{
            
            iconImageView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url) ?? ""),placeholderImage:  UIImage(named: "avatar_default_small"))
            //            nameLabel.text = statusViewModel?.cellArray.status.user.name
            nameLabel.text = statusViewModel?.status?.user?.name
            memberIconView.image = statusViewModel?.memberImage
            
//            timeLabel.text = statusViewModel?.status?.created_at
            timeLabel.text = statusViewModel?.creatAtText
            sourceLabel.text = statusViewModel?.sourceText
            
            //正文数据
//            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            
            //清空底部约束
            bottomConstraint?.uninstall()
            
            //微博正文的配图
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0{
                
                //有配图
                
                //判断是否为单图,修改 item 的 size
//                if pic_urls.count == 1{
//                    
//                    let originalPic = originalPicView.collectionViewLayout as! UICollectionViewFlowLayout
//                    originalPic.itemSize = statusViewModel!.originalPicViewSize
//                }else{
//                    
//                    let originalPic = originalPicView.collectionViewLayout as! UICollectionViewFlowLayout
//                    originalPic.itemSize = CGSizeMake(itemWH, itemWH)
//                }
//                
//                //加载图片数据
//                //从 CellStatusViewModel 中通过图片的张数 拿到 对应的 size
//                originalPicView.snp_updateConstraints(closure: { (make) -> Void in
//                    
//                    make.size.equalTo(statusViewModel!.originalPicViewSize)
//                })
//                
//                // ***** 设置数据 (根据数据计算出配图视图的大小并设置上去)
//                originalPicView.pictures = pic_urls
                
                //元组一句话搞定
                originalPicView.dataInfo = (pic_urls,statusViewModel!.originalPicViewSize)
                
                //显示配图
                originalPicView.hidden = false
                
                //更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                   bottomConstraint = make.bottom.equalTo(originalPicView).offset(StatusCellMargin).constraint
                })
                
            }else{
                
                //无配图
                
                //隐藏配图
                originalPicView.hidden = true
                
                //更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
//        backgroundColor = RandomColor
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(originalPicView)
        
        //添加约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: StatusCellHeadImgWH, height: StatusCellHeadImgWH))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconImageView)
            make.leading.equalTo(iconImageView.snp_trailing).offset(StatusCellMargin)
        }
        
        memberIconView.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(StatusCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconImageView)
            make.leading.equalTo(nameLabel)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(StatusCellMargin)
        }
        
        originalPicView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.leading.equalTo(contentLabel)
            //假定宽高
            make.size.equalTo(CGSizeMake(100, 100))
            
        }
        
        // 让 OriginalView 的底部约束 等于 正文底部的约束
        self.snp_makeConstraints { (make) -> Void in
            
            bottomConstraint = make.bottom.equalTo(originalPicView).offset(StatusCellMargin).constraint
        }
    }
    
    //懒加载控件
    //头像
    private lazy var iconImageView: UIImageView = {
        
        let iconView = UIImageView(image: UIImage(named: "avatar_default_small"))
        
        iconView.layer.cornerRadius = StatusCellHeadImgWH / 2
        iconView.layer.masksToBounds = true
        
        return iconView
    }()
    //昵称的label
    private lazy var nameLabel: UILabel = {
        
        let name = UILabel(textFont: StatusCellNameFontSize, textColor: UIColor.darkGrayColor())
        
        name.text = "宝宝爱酱"
        
        return name
    }()
    
    //会员图片
    private lazy var memberIconView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    //时间
    private lazy var timeLabel: UILabel = {
        
        let time = UILabel(textFont: StatusCellTimeFontSize, textColor: UIColor.orangeColor())
        
        time.text = "time"
        
        return time
    }()
    
    //来源
    private lazy var sourceLabel: UILabel = {
        
        let time = UILabel(textFont: StatusCellTimeFontSize, textColor: UIColor.darkGrayColor())
        
        time.text = "source"
        
        return time
    }()
    
    //微博正文显示的内容
//    private lazy var contentLabel: UILabel = {
//        
//        let content = UILabel(textFont: StatusCellContentFontSize, textColor: UIColor.darkGrayColor(), preferredMaxLayoutWidth: SCREENW - 2 * StatusCellMargin)
//        
//        content.text = "台州身边事#椒江区海正育才小学位于处在机场路和洪三路的交叉口旁，将于9月1日正式开学，现在正在对外招生中。家有下半年上小学的孩子家长们，可得注意喽！只要孩子是在2009年9月1日至2010年8月31日期间出生的，不限户籍，均可报名"
//        
//        return content
//    }()
    private lazy var contentLabel: YYLabel = {
        //        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * HMStatusCellMargin)
        let label = YYLabel()
        label.userInteractionEnabled = true
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(StatusCellContentFontSize)
        label.preferredMaxLayoutWidth = SCREENW - 2 * StatusCellMargin
        label.numberOfLines = 0
        return label
    }()
    
    //懒加载
    private lazy var originalPicView: RetweetPictureView = RetweetPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
}









